<template>
  <div class="login-container">
    <div class="login-box">
      <div class="title">网球场馆预约管理系统</div>
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="用户名"
            prefix-icon="User"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            placeholder="密码"
            prefix-icon="Lock"
            type="password"
            show-password
          />
        </el-form-item>
        <el-form-item>
          <el-button
            :loading="loading"
            type="primary"
            @click="handleLogin"
            class="login-button"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
      <div class="tips">
        <p>默认管理员账号: admin/123456</p>
        <p>默认用户账号: user/123456</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
import { ElMessage } from "element-plus";
import { login } from "@/api/user";

const router = useRouter();
const store = useStore();
const loginFormRef = ref(null);
const loginForm = reactive({
  username: "",
  password: "",
});
const loading = ref(false);

const loginRules = {
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
};

const handleLogin = () => {
  loginFormRef.value.validate(async (valid) => {
    if (!valid) return;

    loading.value = true;
    try {
      // 调用登录接口
      const response = await login({
        username: loginForm.username,
        password: loginForm.password,
      });

      if (response && response.code === 200) {
        console.log(JSON.stringify(response));
        // 调试信息，可移除
        // debugger;

        const { data } = response;

        // 登录成功，保存token和用户信息
        localStorage.setItem("token", data.token);
        store.commit("user/SET_TOKEN", data.token);
        store.commit("user/SET_USER", data.user);

        ElMessage.success("登录成功");

        // 登录成功后跳转到首页
        router.push({ path: "/" });
      } else {
        ElMessage.error(response.msg || "登录失败，请检查用户名和密码");
      }
    } catch (error) {
      console.error("登录错误:", error);
      ElMessage.error(error.message || "登录失败，请稍后重试");
    } finally {
      loading.value = false;
    }
  });
};
</script>

<style lang="scss" scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7f9;

  .login-box {
    width: 400px;
    padding: 30px;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);

    .title {
      font-size: 22px;
      text-align: center;
      margin-bottom: 30px;
      color: #2c3e50;
      font-weight: bold;
    }

    .login-button {
      width: 100%;
    }

    .tips {
      margin-top: 20px;
      text-align: center;
      color: #909399;
      font-size: 13px;
    }
  }
}
</style>
